package com.ljw.punishment.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ljw.punishment.baserule.CommonRule;
import com.ljw.punishment.entry.ShopRuleInfoDAO;
import com.ljw.punishment.rulefactory.CustomRuleFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.io.StringReader;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lijunwei
 * @date 2025/5/21
 */
@Slf4j
public class RuleLoaderServiceImpl {

    public Set<CommonRule> createRuleByGroupId(String rewardCode, Integer groupId) throws Exception {
        Set<CommonRule> rules = new HashSet<>();

        //shop_rule_info
       // List<ShopRuleInfoDAO> shopRuleInfoDoS = shopRuleInfoRepository.queryShopRuleByCode(rewardCode, groupId);

        String shopRuleInfoDoSstr = "[{\"description\":\"规则描述: 规则编码check20250519160417339规则场景名称： " +
                "关键规则规则内容规则描述：检查创建时是否是新店等于1 门店同季度触犯次数等于1\",\"id\":4240," +
                "\"name\":\"关键规则规则模块b1b1907d0ac34b9dabc9a82fb8206b43\",\"rewardCode\":\"check20250519160417339\"," +
                "\"ruleContext\":\"[{\\\"condition\\\":\\\"ruleCreateShop==1 &&sameSeasonShopPunishmentNum==1 " +
                "\\\",\\\"actions\\\":[\\\"\\\"],\\\"description\\\":\\\"规则描述：检查创建时是否是新店等于1 门店同季度触犯次数等于1\\\"}]\"," +
                "\"ruleGroupId\":2157,\"rulePriority\":2147483647},{\"description\":\"拓展规则描述: " +
                "规则编码check20250519160417339规则场景名称： 关键规则规则内容拓展规则描述：操作人包含{\\\"supervisor\\\",\\\"onlineQuality\\\"," +
                "\\\"visitQuality\\\"} 检查项包含{859,4325000086}\",\"id\":4244," +
                "\"name\":\"关键规则规则扩展模块9bab31c0d97244eab00f6bc1ac89dfb7\"," +
                "\"rewardCode\":\"check20250519160417339\"," +
                "\"ruleContext\":\"[{\\\"condition\\\":\\\"{\\\\\\\"supervisor\\\\\\\"," +
                "\\\\\\\"onlineQuality\\\\\\\",\\\\\\\"visitQuality\\\\\\\"} contains operator&&{859,4325000086} " +
                "contains checkItemId\\\",\\\"actions\\\":[\\\"\\\"]," +
                "\\\"description\\\":\\\"拓展规则描述：操作人包含{\\\\\\\"supervisor\\\\\\\",\\\\\\\"onlineQuality\\\\\\\"," +
                "\\\\\\\"visitQuality\\\\\\\"} 检查项包含{859,4325000086}\\\"}]\",\"ruleGroupId\":2157," +
                "\"rulePriority\":2147483647}]\n";
        List<ShopRuleInfoDAO> shopRuleInfoDoS = JSON.parseObject(shopRuleInfoDoSstr,
                new TypeReference<List<ShopRuleInfoDAO>>() {
                });
        if (CollectionUtils.isEmpty(shopRuleInfoDoS)) {
            return rules;
        }

        for (ShopRuleInfoDAO shopRuleInfoDAO : shopRuleInfoDoS) {

            CustomRuleFactory customRuleFactory = new CustomRuleFactory(CustomRuleFactory.createJSONReader(), CustomRuleFactory.initParseContext());

            CommonRule rule = (CommonRule) customRuleFactory.createRule(new StringReader(shopRuleInfoDAO.getRuleContext()));

            rule.setPriority(shopRuleInfoDAO.getRulePriority());
            rule.setId(shopRuleInfoDAO.getId());
            rule.setName(shopRuleInfoDAO.getName());
            rule.setRewardCode(shopRuleInfoDAO.getRewardCode());
            rule.setDescription("CommonRule : {  id = " + shopRuleInfoDAO.getId() + ", name = " + shopRuleInfoDAO.getName() + ",rewardCode = " + shopRuleInfoDAO.getRewardCode() + "}");
            rules.add(rule);
        }
        log.info("从数据库加载规则,rewardCode={},groupId={} rule：{}", rewardCode, groupId, rules);
        System.out.println("rules(CommonRule):"+rules);
        return rules;
    }
}
